/*
* @Author: baidwwy
* @Date:   2019-04-08 06:01:04
* @Last Modified by:   baidwwy
* @Last Modified time: 2020-05-17 15:51:28
*/
#include "gge.h"
#include "SDL_mouse.h"

static int	LUA_GetGlobalMouseState(lua_State *L)
{
	int x, y;
	int r = SDL_GetGlobalMouseState(&x, &y);
	lua_pushinteger(L, r);
	lua_pushinteger(L, x);
	lua_pushinteger(L, y);
	
	return 3;
}

static int	LUA_GetRelativeMouseState(lua_State *L)
{
	int x, y;
	int r = SDL_GetRelativeMouseState(&x, &y);
	lua_pushinteger(L, r);
	lua_pushinteger(L, x);
	lua_pushinteger(L, y);
	
	return 3;
}

static int	LUA_WarpMouseGlobal(lua_State *L)
{
	int x		= (int)luaL_checkinteger(L, 1);
	int y		= (int)luaL_checkinteger(L, 2);

	SDL_WarpMouseGlobal(x, y);
	return 0;
}

static int	LUA_SetRelativeMouseMode(lua_State *L)
{
	SDL_bool enabled = (SDL_bool)lua_toboolean(L, 1);

	lua_pushboolean(L,SDL_SetRelativeMouseMode(enabled) == 0);
	return 1;
}

static int	LUA_CaptureMouse(lua_State *L)
{
	SDL_bool enabled = (SDL_bool)lua_toboolean(L, 1);

	lua_pushboolean(L,SDL_CaptureMouse(enabled) == 0);
	return 1;
}

static int	LUA_GetRelativeMouseMode(lua_State *L)
{
	lua_pushboolean(L,SDL_GetRelativeMouseMode()== 0);
	return 1;
}

static int	LUA_CreateCursor(lua_State *L)
{
	const Uint8 *data	= (const Uint8 *)luaL_checkstring(L, 1);
	const Uint8 *mask	= (const Uint8 *)luaL_checkstring(L, 2);
	int w			= (int)luaL_checkinteger(L, 3);
	int h			= (int)luaL_checkinteger(L, 4);
	int hot_x		= (int)luaL_checkinteger(L, 5);
	int hot_y		= (int)luaL_checkinteger(L, 6);
	SDL_Cursor **ud;
	SDL_Cursor *c = SDL_CreateCursor(data, mask, w, h, hot_x, hot_y);

	ud = (SDL_Cursor**)lua_newuserdata(L, sizeof (SDL_Cursor*));
	*ud = c;
	luaL_setmetatable(L, "SDL_Cursor");
	return 1;
}

static int	LUA_CreateSystemCursor(lua_State *L)
{
	int id	= (int)luaL_checkinteger(L, 1);
	SDL_Cursor *c;
	SDL_Cursor **ud;
	c = SDL_CreateSystemCursor(id);

	ud = (SDL_Cursor**)lua_newuserdata(L, sizeof (SDL_Cursor*));
	*ud = c;
	luaL_setmetatable(L, "SDL_Cursor");
	return 1;
}

static int	LUA_SetCursor(lua_State *L)
{
	SDL_Cursor * c = *(SDL_Cursor**)luaL_checkudata(L, 1, "SDL_Cursor");
	SDL_SetCursor(c);
	return 0;
}

static int	LUA_GetCursor(lua_State *L)
{
	SDL_Cursor *c = SDL_GetCursor();
	SDL_Cursor **ud = (SDL_Cursor**)lua_newuserdata(L, sizeof (SDL_Cursor*));
	*ud = c;
	luaL_setmetatable(L, "SDL_Cursor");
	return 1;
}

static int	LUA_GetDefaultCursor(lua_State *L)
{
	SDL_Cursor *c = SDL_GetDefaultCursor();
	SDL_Cursor **ud = (SDL_Cursor**)lua_newuserdata(L, sizeof (SDL_Cursor*));
	*ud = c;
	luaL_setmetatable(L, "SDL_Cursor");
	return 1;
}

static int	LUA_FreeCursor(lua_State *L)
{
	SDL_Cursor * c = *(SDL_Cursor**)luaL_checkudata(L, 1, "SDL_Cursor");
	SDL_FreeCursor(c);
	return 0;
}

static int	LUA_ShowCursor(lua_State *L)
{
	int toggle = (int)luaL_opt(L,lua_toboolean ,1,1);
	lua_pushinteger(L,SDL_ShowCursor(toggle));
	return 1;
}

static const luaL_Reg SDL_Meta[] = {
	{"CaptureMouse"          , LUA_CaptureMouse}          ,
	{"CreateCursor"          , LUA_CreateCursor}          ,
	{"CreateSystemCursor"    , LUA_CreateSystemCursor}    ,
	{"GetCursor"             , LUA_GetCursor}             ,
	//{"GetDefaultCursor"    , LUA_GetDefaultCursor}      ,
	{"GetGlobalMouseState"   , LUA_GetGlobalMouseState}   ,

	{"GetRelativeMouseMode"  , LUA_GetRelativeMouseMode}  ,
	{"GetRelativeMouseState" , LUA_GetRelativeMouseState} ,
	{"SetRelativeMouseMode"  , LUA_SetRelativeMouseMode}  ,
	{"ShowCursor"            , LUA_ShowCursor}            ,
	{"WarpMouseGlobal"       , LUA_WarpMouseGlobal}       ,
	//LUA_SetCursor
	{ NULL,	NULL}
};

void OPEN_Mouse(lua_State *L)
{

	luaL_setfuncs(L,SDL_Meta,0);
}